package com.zhongxi.guoyuan.view;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.fragment.MainPersonFragment;
import com.zhongxi.guoyuan.fragment.MainRewardCoinFragment;
import com.zhongxi.guoyuan.fragment.MainStoreFragment;
import com.zhongxi.guoyuan.fragment.NewPersonGiftFragment;
import com.zhongxi.guoyuan.OffLineShop.OffLineShopFragment;
import com.zhongxi.guoyuan.fragment.beautifulStoreOrder.StoreOrderHasNoApplyFragment;
import com.zhongxi.guoyuan.view.beautifulStore.ProjectDetailActivity;
import com.zhongxi.guoyuan.view.user.LoginActivity;

public class MainActivity extends BaseActivity {

    private Fragment[] fragments;
    private MainStoreFragment firstFragment;
    private MainRewardCoinFragment secondFragment;
    private OffLineShopFragment thirdFragment;
    private MainPersonFragment forthFragment;
    private int currentIndex = 0, index = -1;
    private RelativeLayout relaHome;
    private Button[] tabs;
    public static final int REQUEST_CODE = 100;
    private NewPersonGiftFragment fiveFragment;

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_main);
        tabs = new Button[5];
        tabs[0] = (Button) findViewById(R.id.tab0);
        tabs[1] = (Button) findViewById(R.id.tab1);
        tabs[2] = (Button) findViewById(R.id.tab2);
        tabs[3] = (Button) findViewById(R.id.tab3);
        tabs[4] = (Button) findViewById(R.id.tab4);
        relaHome = (RelativeLayout) findViewById(R.id.relaHome);
        firstFragment = new MainStoreFragment();
        secondFragment = new MainRewardCoinFragment();
        thirdFragment = new OffLineShopFragment();
        forthFragment = new MainPersonFragment();
        fiveFragment = new NewPersonGiftFragment();
        fragments = new Fragment[]{firstFragment, secondFragment, thirdFragment, forthFragment, fiveFragment};
        index = getIntent().getIntExtra("index", -1);
        if (index == 0) {
            getSupportFragmentManager().beginTransaction().add(R.id.relaContainer, fragments[0]).commit();
            tabs[0].setSelected(true);
        } else {
            refresh();
        }
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        relaHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, MainHomeActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    private void refresh() {
        if (index != currentIndex) {
            FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
            if (fragments[currentIndex].isAdded()) {
                trx.hide(fragments[currentIndex]);
            }
            if (!fragments[index].isAdded()) {
                trx.add(R.id.relaContainer, fragments[index]);
            }
            trx.show(fragments[index]).commit();
            tabs[index].setSelected(true);
            tabs[currentIndex].setSelected(false);
            currentIndex = index;
        }
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tab0:
                index = 0;
                break;
            case R.id.tab1:
                index = 1;
                break;
            case R.id.tab2:
                index = 2;
                break;
            case R.id.tab3:
                if (null == MyApplication.getInstance().getUserId() || "".equals(MyApplication.getInstance().getUserId())) {
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.putExtra("position", 1);
                    startActivity(intent);
                } else {
                    index = 3;
                }
                break;
            case R.id.tab4:
                if (null == MyApplication.getInstance().getUserId() || "".equals(MyApplication.getInstance().getUserId())) {
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.putExtra("position", 1);
                    startActivity(intent);
                } else {
                    index = 4;
                }
                break;
        }
        refresh();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && resultCode == ProjectDetailActivity.RESULT_CODE) {
            index = 1;
            refresh();
        }else if (resultCode == StoreOrderHasNoApplyFragment.RESULTCODE) {
            index = 1;
            refresh();
        }

    }

}
